﻿using DLL.NET50.DB.MS.Entity;

using Microsoft.EntityFrameworkCore;
using System;

namespace DLL.NET50.MS.Context
{
    public class CMSContext : DbContext
    {
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            if (!optionsBuilder.IsConfigured)
            {
                optionsBuilder.UseMySQL("Server=localhost;Database=msrobot;User ID=root;Password=123456;Connection Timeout=43200;SslMode=None")
                    .UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking);
                //optionsBuilder.AutomaticMigrationDataLossAllowed = true;
            }
        }

        public virtual DbSet<adi_dic_drug> adi_dic_drug { get; set; }
        public virtual DbSet<adi_FlushGroup> adi_flushGroups { get; set; }
        public virtual DbSet<adi_FlushParam> adi_flushParams { get; set; }
        public virtual DbSet<adi_PumpBackParam> adi_PumpBackParams { get; set; }
        public virtual DbSet<adi_work_file> adi_work_file { get; set; }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            //配置药品，和冲刷参数 的一对多关系
            //配置药品，和冲刷参数 的一对多关系
            modelBuilder.Entity<adi_FlushParam>()
                .HasOne(d => d.flushGroup)
                .WithMany(fp => fp.flush_Params)
                .HasForeignKey(f => f.DrugFlushGroupCode)
                .OnDelete(DeleteBehavior.Cascade);

            modelBuilder.Entity<adi_PumpBackParam>()
                .HasOne(d => d.flushGroup)
                .WithMany(fp => fp.pumpBack_Params)
                .HasForeignKey(f => f.DrugFlushGroupCode)
                .OnDelete(DeleteBehavior.Cascade);

            modelBuilder.Entity<adi_FlushGroup>()
                .HasOne(d => d.drug)
                .WithMany(fg => fg.flush_Groups)
                .HasForeignKey(fg => fg.Drug_UniqueCode)
                .OnDelete(DeleteBehavior.Cascade);

            modelBuilder.Entity<adi_dic_drug>()
                .HasKey(d => d.Drug_UniqueCode);
            base.OnModelCreating(modelBuilder);
        }
    }
}